Public Class Duplicates

    Public Event DuplicatesUpdated()

    Private Const FileIdCell As Integer = 4

    Private Const ConfirmationMessage As String = "You are about to delete {0} rows. This operation cannot be undone. Do you wish to continue?"

    Private Sub Duplicates_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.FilesTableAdapter.FillByDuplicates(Me.Products.Files)

    End Sub


    Private Sub UpdateDetails_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseForm.Click

        RaiseEvent DuplicatesUpdated()
        Me.Close()

    End Sub

    Private Sub DuplicatesGrid_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles DuplicatesGrid.KeyDown

        If e.KeyCode = Keys.Delete Then
            e.SuppressKeyPress = True
        End If

    End Sub

    Private Sub DeleteSelected_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteSelected.Click

        Try
            Dim faAdapter As New FileArtistDataTableAdapters.FileArtistTableAdapter
            Dim irAdapter As New InstalledRuntimesDataTableAdapters.InstalledRuntimesTableAdapter
            Dim fptAdapater As New FileProductTypesDataTableAdapters.FileProductTypeTableAdapter
            Dim ffAdapter As New FileForFigureDataTableAdapters.FileForFigureTableAdapter

            Dim fileId As Integer = -1

            Dim response As DialogResult = MessageBox.Show(String.Format(ConfirmationMessage, DuplicatesGrid.SelectedRows.Count), _
                                                           "Delete row(s)?", _
                                                               MessageBoxButtons.YesNo, _
                                                               MessageBoxIcon.Question, _
                                                               MessageBoxDefaultButton.Button2)
            If (response = DialogResult.Yes) Then
                For Each row As DataGridViewRow In DuplicatesGrid.SelectedRows
                    fileId = CType(row.Cells(FileIdCell).Value, Integer)
                    faAdapter.DeleteAllForFile(fileId)
                    irAdapter.DeleteAllForFile(fileId)
                    fptAdapater.DeleteAllForFile(fileId)
                    ffAdapter.DeleteAllForFile(fileId)
                    DuplicatesGrid.Rows.Remove(row)
                Next
                ProductsDS.EndEdit()
                FilesTableAdapter.Update(Products.Files)
            End If

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

    End Sub

End Class